import { configureStore } from '@reduxjs/toolkit';
import { persistStore, persistReducer } from 'redux-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { combineReducers } from 'redux';
import autoScrollReducer from './slices/autoScrollSlice';

// 配置持久化
const persistConfig = {
  key: 'root',
  storage: AsyncStorage,
  whitelist: ['autoScroll'], // 只持久化autoScroll
  blacklist: ['isLoading', 'error'] // 不持久化临时状态
};

// 合并reducer
const rootReducer = combineReducers({
  autoScroll: autoScrollReducer,
});

// 创建持久化的reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

// 创建store
export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: ['persist/PERSIST', 'persist/REHYDRATE'],
      },
    }),
});

// 创建persistor
export const persistor = persistStore(store);

// 类型定义（使用类型断言避免TypeScript错误）
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

// 为了兼容性，也导出JS版本
export const getStore = () => store;